
package cn.com.xy.sms.sdk.db.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;

import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.DBManager;
import cn.com.xy.sms.sdk.db.XyCursor;
import cn.com.xy.sms.sdk.db.base.BaseManager;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.StringUtils;

public class CountSceneManager {

    public static final String SCENE_ID = "scene_id";

    public static final String COUNT = "count";

    public static final String TABLE_NAME = "tb_count_scene";

    public static final String DROP_TABLE = " DROP TABLE IF EXISTS " + TABLE_NAME;

    public static final String CREATE_TABLE = "create table  if not exists " + TABLE_NAME + " ("

    + SCENE_ID + " TEXT," + COUNT + " INT" + ")";

    public static List<Sceneconfig> queryAllSceneconfig() {
        List<Sceneconfig> sceneconfigList = new ArrayList<Sceneconfig>();
        XyCursor cur = null;
        try {
            cur = DBManager.query(TABLE_NAME, new String[] {
                SCENE_ID, COUNT
            }, null, null);
            if (cur != null && cur.getCount() > 0) {
                int sceneIdIndex = cur.getColumnIndex(SCENE_ID);
                int countIndex = cur.getColumnIndex(COUNT);
                Sceneconfig tempConfig = null;
                while (cur.moveToNext()) {
                    tempConfig = new Sceneconfig();
                    tempConfig.sceneId = cur.getString(sceneIdIndex);
                    tempConfig.count = cur.getInt(countIndex);
                    sceneconfigList.add(tempConfig);
                }

            }
        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "queryAllSceneconfig: ", e);
            /*SDK-550 hewengao 20160719 end*/
        } finally {
            XyCursor.closeCursor(cur, true);
        }
        return sceneconfigList;
    }

//    public static int queryCountBySceneId(String scene_id) {
//        XyCursor cur = null;
//        try {
//        	
//            cur = DBManager.query(TABLE_NAME, new String[] {
//                SCENE_ID, COUNT
//            }, SCENE_ID + " = ? ", new String[] {
//                scene_id + ""
//            }, null, null, null, "1");
//            if (cur != null && cur.getCount() > 0) {
//                int countIndex = cur.getColumnIndex(COUNT);
//                int sceneIdIndex = cur.getColumnIndex(SCENE_ID);
//                Sceneconfig tempConfig = null;
//                while (cur.moveToNext()) {
//                    tempConfig = new Sceneconfig();
//                    tempConfig.count = cur.getInt(countIndex);
//                    tempConfig.sceneId = cur.getString(sceneIdIndex);
//
//                }
//                return tempConfig.count;
//            }
//        } catch (Throwable e) {
//            e.printStackTrace();
//        } finally {
//            if (cur != null) {
//                cur.close(true);
//                cur=null;
//            }
//        }
//        return 0;
//    }

    public static long insertOrUpdate(HashMap<String,String> paramsMap) {
    	String sceneId = paramsMap.get("titleNo");
    
    	long result = -1;
    	int count =1;
//		Sceneconfig sceneconfig = new Sceneconfig();
//		sceneconfig.sceneId = sceneId;
//        sceneconfig.count = 1;//初始化次数为1
        ContentValues values = null;
        XyCursor xyCursor = null;
        try {
                 
            if (!StringUtils.isNull(sceneId)) {
                xyCursor = DBManager.query(TABLE_NAME, new String[] {
                		SCENE_ID, COUNT
                }, SCENE_ID + " = ? ", new String[] {
                		sceneId
                });
            }
            if (xyCursor != null && xyCursor.getCount() > 0) {
            	xyCursor.moveToNext();
                int countIndex = xyCursor.getColumnIndex(COUNT);
                count = xyCursor.getInt(countIndex) + 1;
                values=BaseManager.getContentValues(null, "scene_id",sceneId,"count",String.valueOf(count));
                //更新解析次数
            	DBManager.update(TABLE_NAME, values, SCENE_ID+" = ? ", new String[]{
            	        sceneId});
            	
            	return 0;
            } else {
            	//插入一条记录
                values=BaseManager.getContentValues(null, "scene_id",sceneId,"count",String.valueOf(count));
                result = DBManager.insert(TABLE_NAME,  values);
            }
            
        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "insertOrUpdate: ", e);
            /*SDK-550 hewengao 20160719 end*/
        } finally {
            XyCursor.closeCursor(xyCursor, true);
        }
        return result;
    }
    public static void deleteAll() {
        try {
            DBManager.delete(TABLE_NAME, null, null);

        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "deleteAll: ", e);
            /*SDK-550 hewengao 20160719 end*/
        }
    }

    public static void deleteSceneconfigById(String scenid) {
        try {
            DBManager.delete(TABLE_NAME, SCENE_ID + " = ?", new String[] {
                scenid
            });
        } catch (Throwable e) {
//            e.printStackTrace();
            /*SDK-550 hewengao 20160719 start*/
            LogManager.w(Constant.TAG, "deleteSceneconfigById: ", e);
            /*SDK-550 hewengao 20160719 end*/
        }
    }

   

}
